package de.seminar.lego3D.fragments;

import de.seminar.lego3D.Constants;
import de.seminar.lego3D.R;
import android.app.FragmentTransaction;
import android.app.ListFragment;
import android.content.Context;
import android.os.Bundle;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.BaseAdapter;
import android.widget.ImageView;
import android.widget.ListView;

/**
 * Fragment showing a list of categories. Each category opens another fragment.
 * @author Christian Lander
 *
 */
public class LegoOptionsFragment extends ListFragment {

	/**
	 * the current position = selected item
	 */
	int mCurCheckPosition = 0;

	@Override
	public void onActivityCreated(Bundle savedInstanceState) {
		super.onActivityCreated(savedInstanceState);

		// populate the list with the help of my own adapter
		setListAdapter(new MyListAdapter(getActivity()));

		if (savedInstanceState != null) {
			// Restore last state for checked position.
			mCurCheckPosition = savedInstanceState.getInt(Constants.CUR_CHOICE,
					0);
		}

		// the list view highlights the selected item.
		getListView().setChoiceMode(ListView.CHOICE_MODE_SINGLE);
		// Make sure our UI is in the correct state.
		showDetails(mCurCheckPosition);

	}

	@Override
	public void onSaveInstanceState(Bundle outState) {
		super.onSaveInstanceState(outState);
		outState.putInt(Constants.CUR_CHOICE, mCurCheckPosition);
	}

	@Override
	public void onListItemClick(ListView l, View v, int position, long id) {
		showDetails(position);
	}

	/**
	 * Helper function to show the details of a selected item, either by
	 * displaying a fragment in-place in the current UI, or starting a whole new
	 * activity in which it is displayed.
	 * @param index - the index of the fragment
	 */
	void showDetails(int index) {
		mCurCheckPosition = index;
		Constants.CUR_FRAG = mCurCheckPosition;
		// We can display everything in-place with fragments, so update
		// the list to highlight the selected item and show the data.
		getListView().setItemChecked(index, true);

		switch (mCurCheckPosition) {
		case 0:
			//lego view
			DetailsFragment legoFragment = (DetailsFragment) getFragmentManager().findFragmentById(R.id.details_frag);
			if (legoFragment == null || legoFragment.getShownIndex() != index) {
                // Make new fragment to show this selection.
				legoFragment = LegoBricksFragment.newInstance(index);

                // Execute a transaction, replacing any existing fragment
                // with this one inside the frame.
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.details_frag, legoFragment);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }

			break;
		case 1:
			// color view
			DetailsFragment colorFragment = (DetailsFragment) getFragmentManager().findFragmentById(R.id.details_frag);
			if (colorFragment == null || colorFragment.getShownIndex() != index) {
                // Make new fragment to show this selection.
				colorFragment = BricksColorFragment.newInstance(index);

                // Execute a transaction, replacing any existing fragment
                // with this one inside the frame.
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.details_frag, colorFragment);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }

			break;
		case 2:
			// server connection view
			DetailsFragment settingsFragment = (DetailsFragment) getFragmentManager().findFragmentById(R.id.details_frag);
			if (settingsFragment == null || settingsFragment.getShownIndex() != index) {
                // Make new fragment to show this selection.
				settingsFragment = SettingsFragment.newInstance(index);

                // Execute a transaction, replacing any existing fragment
                // with this one inside the frame.
                FragmentTransaction ft = getFragmentManager().beginTransaction();
                ft.replace(R.id.details_frag, settingsFragment);
                ft.setTransition(FragmentTransaction.TRANSIT_FRAGMENT_FADE);
                ft.commit();
            }
			break;
		default:
			break;
		}
	}

	/**
	 * Custom adapter for the list view in this fragment.
	 * 
	 * @author christian
	 *
	 */
	private class MyListAdapter extends BaseAdapter {

		/**
		 * context of the main activity
		 */
		private Context ctx;
		
		/**
		 * Creates a new list adapter
		 */
		public MyListAdapter(Context context) {
			ctx = context;
		}
		
		@Override
		public int getCount() {
			return Constants.list_items.length;
		}

		@Override
		public Object getItem(int position) {
			return Constants.list_items[position];
		}

		@Override
		public long getItemId(int position) {
			return  Constants.list_items[position];
		}

		@Override
		public View getView(int position, View contentView, ViewGroup parent) {
			View v = LayoutInflater.from(ctx).inflate(R.layout.frag_list_item, null, false);
			ImageView img = (ImageView) v.findViewById(R.id.list_image);
			img.setImageResource(Constants.list_items[position]);
			return v;
		}
		
	}
}
